//
//  RetweetBlogView.swift
//  SinaBlog
//
//  Created by 有何不可 on 16/1/7.
//  Copyright © 2016年 有何不可. All rights reserved.
//

import UIKit
import SnapKit

class RetweetBlogView: UIView {
    
    var retBlogConstraint: Constraint?
    
    var retweetBlogViewModel: PersonalBlogViewModel? {
        
        didSet {
          
            textLal.text = retweetBlogViewModel?.statuse?.retweeted_status?.text
            
            retBlogConstraint?.uninstall()
            
            if retweetBlogViewModel?.statuse?.retweeted_status?.pic_urls?.count > 0 {
                
                pictureView.hidden = false
                pictureView.picUrls = retweetBlogViewModel?.statuse?.retweeted_status?.pic_urls
                
                snp_updateConstraints(closure: { (make) -> Void in
                    retBlogConstraint =  make.bottom.equalTo(pictureView.snp_bottom).offset(OriginalBlogMargin).constraint
                })
            } else {
                
                pictureView.hidden = true
                snp_updateConstraints(closure: { (make) -> Void in
                    retBlogConstraint =  make.bottom.equalTo(textLal.snp_bottom).offset(OriginalBlogMargin).constraint
                })

            }
           
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 240 / 255, alpha: 1.0)
        setUI()
    }
    
    private func setUI() {
        
        addSubview(textLal)
        addSubview(pictureView)
        
        textLal.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(OriginalBlogMargin)
            make.width.equalTo(self.snp_width).offset(-(2 * OriginalBlogMargin))
            make.top.equalTo(self.snp_top).offset(OriginalBlogMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLal.snp_bottom).offset(OriginalBlogMargin)
            make.left.equalTo(self.snp_left).offset(OriginalBlogMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
           retBlogConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var textLal: UILabel = {
       
        let lal = UILabel()
        
        lal.font = UIFont.systemFontOfSize(14)
        
        lal.text = "牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋牛犀通是笨蛋"
        
        lal.numberOfLines = 0
        
        lal.textColor = UIColor.blackColor()
        
        return lal
    }()
    
    private lazy var pictureView = PictureView()
}
